package com.platform.modules.mall.activity.rule;


import com.platform.common.exception.BusinessException;
import com.platform.common.utils.JedisUtil;
import com.platform.modules.mall.activity.entity.bo.ActivityGoshoppingBo;
import com.platform.modules.mall.activity.type.ActivityTypeInterface;
import com.platform.modules.mall.activity.type.DirectGoodsType;
import com.platform.modules.mall.activity.type.DrawGoodsType;
import com.platform.modules.mall.activity.type.PollingGoodsType;
import com.platform.modules.mall.entity.MallActivityEntity;
import com.platform.modules.mall.entity.MallGoodsToActivityEntity;
import com.platform.modules.mall.service.MallGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RuleFactory {

    @Autowired
    private JedisUtil jedisUtil;

    @Autowired
    private DrawGoodsType drawGoodsType;
    @Autowired
    private PollingGoodsType pollingGoodsType;
    @Autowired
    private DirectGoodsType directGoodsType;

    public RuleAbstract getRule(MallActivityEntity activity, List<MallGoodsToActivityEntity> list, ActivityGoshoppingBo bo){
        if (ActivityType.DIRECT_GOODS.getType().equals(activity.getActivityType())) {
            return new DirectGoodsUtil(list, bo.getGoodsId());
        }  else if (ActivityType.POLLING_GOODS.getType().equals(activity.getActivityType())) {
            return new PollingGoodsUtil(list, jedisUtil, activity.getId());
        }else if (ActivityType.BI_ZHONG.getType().equals(activity.getActivityType())) {
            return new DrawGoodsUtil(list);
        }else if (ActivityType.FEI_BI_ZHONG.getType().equals(activity.getActivityType())) {
            return new DrawGoodsUtil(list);
        }
        throw new BusinessException("没有匹配的规则");
    }


    public ActivityTypeInterface getRuleNew(MallActivityEntity activity, List<MallGoodsToActivityEntity> list, ActivityGoshoppingBo bo){
        if (ActivityType.DIRECT_GOODS.getType().equals(activity.getActivityType())) {
            directGoodsType.setEntity(list);
            directGoodsType.setData(bo.getGoodsId());
            return directGoodsType;
        }  else if (ActivityType.POLLING_GOODS.getType().equals(activity.getActivityType())) {
            pollingGoodsType.setEntity(list);
            pollingGoodsType.setData(activity.getId());
            return pollingGoodsType;
        }else if (ActivityType.BI_ZHONG.getType().equals(activity.getActivityType())) {
            drawGoodsType.setEntity(list);
            return drawGoodsType;
        }
        throw new BusinessException("没有匹配的规则");
    }



    public enum ActivityType {
        /**
         * 指定商品
         */
        DIRECT_GOODS(1),
        /**
         * 轮询商品
         */
        POLLING_GOODS(2),

        /**
         * 必中抽奖
         */
        BI_ZHONG(3),


        /**
         * 非必中抽奖
         */
        FEI_BI_ZHONG(4);


        private Integer type;

        ActivityType(Integer type) {
            this.type = type;
        }

        public Integer getType() {
            return this.type;
        }
    }
}
